package com.geeksaga.dao.jdbc.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.geeksaga.dao.PrimaryCodeDao;
import com.geeksaga.dao.jdbc.BaseJdbcDao;
import com.geeksaga.domain.PrimaryCode;

/**
 * @author newbie
 * @since 0.1
 */
@Repository
public class PrimaryCodeDaoImpl extends BaseJdbcDao implements PrimaryCodeDao {

    public PrimaryCode add(PrimaryCode primaryCode) throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("code.addPrimary");
        
        Object[] obj = new Object[] { primaryCode.getPrimaryCode(),
                primaryCode.getLocale(),
                primaryCode.getPrimaryCode(),
                primaryCode.getUseYn()};
  
        getJdbcTemplate().update(sql, obj);
        
        return primaryCode;
    }

    public int count() throws DataAccessException {
        // TODO Auto-generated method stub
        return 0;
    }

    public PrimaryCode find(PrimaryCode primaryCode) throws DataAccessException {
        // TODO Auto-generated method stub
        return null;
    }

    @SuppressWarnings("unchecked")
    public List<PrimaryCode> findList() throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("question.findList");
        
        return (List<PrimaryCode>) getJdbcTemplate().query(sql, getRowMapper());
    }

    public PrimaryCode modify(PrimaryCode primaryCode) throws DataAccessException {
        // TODO Auto-generated method stub
        return null;
    }

    public PrimaryCode remove(PrimaryCode primaryCode) throws DataAccessException {
        // TODO Auto-generated method stub
        return null;
    }

    @SuppressWarnings({ "rawtypes" })
	private RowMapper getRowMapper() {
        RowMapper rowMapper = new RowMapper() {
            public Object mapRow(ResultSet rs, int rownum) throws SQLException {
                PrimaryCode primaryCode = new PrimaryCode();
                primaryCode.setPrimaryCode(rs.getString("PRIMARY_CODE"));
                primaryCode.setLocale(rs.getString("LOCALE"));
                primaryCode.setPrimaryCodeName(rs.getString("PRIMARY_CODE_NAME"));
                primaryCode.setUseYn(rs.getString("USE_YN"));

                return primaryCode;
            }
        };
        
        return rowMapper;
    }
}